package com.example.moonshine.baidumap.View.Activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.moonshine.baidumap.Modle.MsgDetail;
import com.example.moonshine.baidumap.Modle.MsgDetailtwo;
import com.example.moonshine.baidumap.Modle.MsgIndex;
import com.example.moonshine.baidumap.Modle.MsgStaticdata;
import com.example.moonshine.baidumap.Modle.SidSelectData;
import com.example.moonshine.baidumap.Modle.flowOutManhoule;
import com.example.moonshine.baidumap.Modle.initialManhole;
import com.example.moonshine.baidumap.R;
import com.example.moonshine.baidumap.Util.DataDBHepler;
import com.github.zackratos.ultimatebar.UltimateBar;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;

import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import static android.content.ContentValues.TAG;

public class DetaialActivity extends Activity{

    String token;
    TextView manholeId;//井Id
    TextView sensorsize;//传感器个数
    TextView waterLevel;//水位
    TextView flow;//水流量
    TextView permeability;//渗透率
    TextView repairFrequency;//维修次数
    TextView repairState;//维修状态
    TextView receivingTime;//接收任务时间
    TextView  repairman;//维修
    TextView gpsPosition;//地址
    TextView State;
    TextView FailureTime;
    String URL = "http://123.249.28.108:8082/pipe-network/DetailedInformation";
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.task_detail);
        UltimateBar.newColorBuilder()
                .statusColor(Color.parseColor("#003768"))       // 状态栏颜色
                .statusDepth(50)                // 状态栏颜色深度
                .build(this)
                .apply();
        initview();
        DataDBHepler dataDBHepler = new DataDBHepler(getBaseContext());
        ArrayList<SidSelectData> DataList = dataDBHepler.FindTokenData();
        final SidSelectData data = new SidSelectData(DataList.get(0).getId(), DataList.get(0).getToken());
        token = data.getToken();//获取数据库里的token
        init();
        Button button = findViewById(R.id.gps_btn);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(DetaialActivity.this, NavigationActivity.class);
                startActivity(intent);
            }
        });

    }
    public void  initview(){
        manholeId = findViewById(R.id.manholeId);
        waterLevel = findViewById(R.id.waterLevel);
        flow = findViewById(R.id.flow);
        permeability = findViewById(R.id.permeability);
        repairState = findViewById(R.id.repairState);
        receivingTime = findViewById(R.id.receivingTime);
        sensorsize = findViewById(R.id.sensorsize);
        gpsPosition = findViewById(R.id.gpsPosition);
        State = findViewById(R.id.item_guzhang);
        FailureTime = findViewById(R.id.item_guzhangtime);
    }

    private void init() {

        new Thread(new Runnable() {
            @RequiresApi(api = Build.VERSION_CODES.CUPCAKE)
            @Override
            public void run() {
                new AnotherTask().execute("");
            }
        }).start();
//        postAdapter = new PostAdapter(getContext(), postlist);
//        listView.setAdapter(postAdapter);
    }

    @SuppressLint("StaticFieldLeak")
    private class AnotherTask extends AsyncTask<String, Void, String> {
        @Override
        protected void onPostExecute(String result) {
            //对UI组件的更新操作
            Gson gson = new Gson();
            MsgDetail msgData= gson.fromJson(result, MsgDetail.class);
            MsgDetailtwo msgDetailtwo = msgData.getDetailedInformation();
            try {
                MsgIndex listData = msgDetailtwo.getFaultRecordManholeCover();
                if (listData == null) {
                    Toast.makeText(getApplicationContext(),"数据为空",Toast.LENGTH_SHORT).show();
                    return;
                } else {
                         manholeId.setText(listData.getManholeId().toString());
                         waterLevel.setText(listData.getWaterLevel().toString());
                         flow.setText(listData.getFlow().toString());
                         State.setText(listData.getState());
                         FailureTime.setText(listData.getFailureTime());
                         permeability.setText(listData.getPermeability().toString());
                         receivingTime.setText(listData.getReceivingTime().toString());
                         repairState.setText(listData.getRepairState().toString());
                    if (listData.getFlag().equals("0"))
                    {

                        initialManhole initialManhole =listData.getInitialManhole();
                        sensorsize.setText(initialManhole.getSensorsize().toString());
                        gpsPosition.setText(initialManhole.getGpsPosition().toString());

                        MsgStaticdata.setGps(initialManhole.getGpsId());
                    }else {
                        flowOutManhoule flowoutManhoule = listData.getFlowOutManhole();
                        MsgStaticdata.setGps(flowoutManhoule.getGpsId());
                        sensorsize.setText(flowoutManhoule.getSensorsize().toString());
                        gpsPosition.setText(flowoutManhoule.getGpsPosition().toString());
                    }
                }
            }catch (Exception e)
            {
                e.printStackTrace();
            }
            try{
            }catch (Exception e){
                Log.e(TAG, "postlisthttp: ",e );
            }
        }
        @Override
        protected String doInBackground(String... params) {
            //耗时的操作
            String url = "http://123.249.28.108:8082/pipe-network//DetailedInformation";

            Gson gson =new Gson();
            OkHttpClient client = new OkHttpClient();
            MediaType JSON = MediaType.parse("application/json; charset=utf-8");
            FormBody body = new FormBody.Builder()
                    .add("manholeId",MsgStaticdata.getManholeId())
                    .build();
            Request request = new Request.Builder()
                    .addHeader("accessToken",token)
                    .url(url)
                    .post(body)
                    .build();
            String result ="";
            try {
                Response response = client.newCall(request).execute();
                //获取后台传输的额status状态码
                result = response.body().string();
            } catch (IOException e) {
                e.printStackTrace();
                Log.e(TAG, "doInBackground: ",e );
            }
            return result;
        }
    }
}
